﻿using Crawler.DBEntities;
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace Crawler.DataHelper
{
    public static class PostHelper
    {
        public static string GetContentRegex(string strResponse)
        {
            if (new Regex(RegexHelper.regexPostContent1,RegexOptions.Singleline).IsMatch(strResponse, 0))
                return RegexHelper.regexPostContent1;
            else if (new Regex(RegexHelper.regexPostContent2,RegexOptions.Singleline).IsMatch(strResponse, 0))
                return RegexHelper.regexPostContent2;
            else if (new Regex(RegexHelper.regexPostContent3,RegexOptions.Singleline).IsMatch(strResponse, 0))
                return RegexHelper.regexPostContent3;
            else if (new Regex(RegexHelper.regexPostContent4, RegexOptions.Singleline).IsMatch(strResponse, 0))
                return RegexHelper.regexPostContent4;
            return "没有匹配的正则表达式";
        }

        /// <summary>
        /// 获取帖子的标题，还需要改进1.有一个网站不是匹配的第一个；2.去掉尾巴
        /// </summary>
        /// <param name="strResponse"></param>
        /// <returns></returns>
        public static string GetPostTitle(string strResponse)
        {
            MatchCollection matches = new Regex(RegexHelper.regexPostTitle).Matches(strResponse);
            Match match = matches[0];
            string title = match.ToString();
            title = getTitle(strResponse,title);
            return title;

        }
        
        /// <summary>
        /// 去掉标签
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string DeleteHtml(string str)
        {
            string withoutHtml1 = Regex.Replace(str, @"<", "");
            string withoutHtml2 = Regex.Replace(withoutHtml1, @">", "");
            return withoutHtml2;
        }
        /// <summary>
        /// 获取标题
        /// </summary>
        /// <param name="html"></param>
        /// <param name="title"></param>
        /// <returns></returns>
        public static string getTitle(string html, string title)
        {
            string str = title.Substring(0, 5);
            MatchCollection mac = Regex.Matches(html, string.Format(@">{0}.*?<", str));

            string match1 = title;
            foreach (Match everymac in mac)
            {
                if (everymac.Value.Contains("..") == false)
                {
                    if (DeleteHtml(everymac.Value).Length < match1.Length)
                    {
                        match1 = DeleteHtml(everymac.Value);
                    }
                }
            }
            string result = Regex.Replace(match1.Trim(), @"'", "‘");
            return result;
        }

        /// <summary>
        /// 获取帖子的时间
        /// </summary>
        /// <param name="strSponse"></param>
        /// <returns></returns>
        public static DateTime GetPostTime(string strResponse)
        {
            DateTime dt1 = new DateTime();
            DateTime dt2 = new DateTime();
            MatchCollection matches = new Regex(RegexHelper.regexPostTime).Matches(strResponse);
            //取出两个时间
            string date1 = matches[0].ToString();
            string date2 = matches[1].ToString();
            //分离数字
            string[] s1 = date1.Split('-', ':', '/', ' ');
            string[] s2 = date2.Split('-', ':', '/', ' ');
            //转化成int
            List<int> s1ToInt = new List<int>();
            List<int> s2ToInt = new List<int>();
            foreach (var s in s1)
            {
                int time = int.Parse(s);
                s1ToInt.Add(time);
            }
            foreach (var s in s2)
            {
                int time = int.Parse(s);
                s2ToInt.Add(time);
            }
            //给datetime赋值
            if (s1ToInt.Count == 6)
                dt1 = new DateTime(s1ToInt[0], s1ToInt[1], s1ToInt[2], s1ToInt[3], s1ToInt[4], s1ToInt[5]);
            if (s2ToInt.Count == 6)
                dt2 = new DateTime(s2ToInt[0], s2ToInt[1], s2ToInt[2], s2ToInt[3], s2ToInt[4], s2ToInt[5]);
            //比较
            var timestamp = dt1 - dt2;
            if (timestamp.CompareTo(new TimeSpan(0, 0, 0, 0, 0)) < 0)
                return dt1;
            else
                return dt2;
        }
        /// <summary>
        /// 获取帖子的内容
        /// </summary>
        /// <param name="strResponse"></param>
        /// <returns></returns>
        public static string GetPostContent(string strResponse,string contentRegex)
        {
            string contentWithLabel = null;
            string contentWithoutLabel = null;
            MatchCollection matches = new Regex(contentRegex,RegexOptions.Singleline).Matches(strResponse);
            if (matches.Count != 0)
            {
                contentWithLabel = matches[0].ToString();
                contentWithoutLabel = GetContent_NoLabel(contentWithLabel);
            }
            return contentWithoutLabel;
        }
        /// <summary>
        /// 获取没标签的帖子内容
        /// </summary>
        /// <param name="contentWithLabel"></param>
        /// <returns></returns>
        public static string GetContent_NoLabel(string contentWithLabel)
        {
            string temp1 = Regex.Replace(contentWithLabel, @"<[\s\S]*?>", "");
            string temp2 = Regex.Replace(temp1, @"&nbsp", "");
            string Content_NoLabel = Regex.Replace(temp2, @"\s", "");
            return Content_NoLabel;
        }

        /// <summary>
        /// 向表中插入帖子的记录
        /// </summary>
        /// <param name="post"></param>
        /// <returns></returns>
        public static bool InsertPostToDB(ref Post post)
        {
            bool isInsertSucceed = true;
            MysqlHelper helper = new MysqlHelper();
            isInsertSucceed = helper.InsertPost(post);
            return isInsertSucceed;
        }

    }
}
